//
//  AdvancedDetailsVc.m
//  Courses
//
//  Created by xxx on 15/1/14.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "AdvancedDetailsVc.h"

#import "AdvancedCourseDetails.h"
#import "CoursedInstructorVc.h"
#import "CourseArrangementVc.h"

#import "CourseClass.h"
#import "User.h"
#import "SignInViewController.h"
#import "ConfirmBillVc.h"

#import "UIImageView+WebCache.h"
#import "NSDate+WTF.h"

#import "CourseIntroductionVC.h"
#import "MobClick.h"
#import "PxhLabel.h"
#import "WTFFonts.h"
@interface AdvancedDetailsVc() <UITableViewDataSource, UITableViewDelegate>

@end

@implementation AdvancedDetailsVc
{
    UIImageView *_imgViewTeacherAvatar;
    UILabel *_lblTeacherName;
    
    AdvancedCourseDetails *_courseDetails;
    UILabel *_lblClassPrice;
    
    UIButton *_btnCls1;
    UIButton *_btnCls2;
    UIButton *_btnCls3;
    int      _kInstructorDetailViewHeight;
}

static const CGFloat kVideoViewHeight = 10;
static const CGFloat kInstructorDetailViewHeight = 90;
static const CGFloat kCourseClassesViewHeight = 60;
static const CGFloat kNavTableHeight = 100;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"课程";
    
//    [self addVideoView];
    
    [self addInstructorDetailView];
    
    [self addCourseClassesView];
    
    [self loadData];
    
    [self addNavTable];
}

//- (void)addVideoView
//{
//    UIView *videoBackgroundView = [[UIView alloc] initWithFrame:AutoRectMake(0, 0, DEFAULT_WIDTH, kVideoViewHeight, WTFAutoLayoutTypePic)];
//    videoBackgroundView.backgroundColor = [UIColor blackColor];
//    
//    [self.view addSubview:videoBackgroundView];
//}

- (void)addInstructorDetailView
{
    UIView *viewInstructor = [[UIView alloc] initWithFrame:AutoRectMake(0, kVideoViewHeight, DEFAULT_WIDTH, kInstructorDetailViewHeight, WTFAutoLayoutTypePic)];
    [self.view addSubview:viewInstructor];
    
    _imgViewTeacherAvatar = [[UIImageView alloc] initWithFrame:AutoRectMake(20, 13, 60, 60, WTFAutoLayoutTypePic)];
    _imgViewTeacherAvatar.layer.cornerRadius = AutoFloat(30);
    _imgViewTeacherAvatar.contentMode = UIViewContentModeScaleAspectFill;
    _imgViewTeacherAvatar.clipsToBounds = YES;
    [viewInstructor addSubview:_imgViewTeacherAvatar];
    
    _lblTeacherName = [[UILabel alloc] initWithFrame:AutoRectMake(100, 10, 120, 30, WTFAutoLayoutTypeBar)];
    [viewInstructor addSubview:_lblTeacherName];
    
    //课程名称显示
    UILabel* courseName = [[UILabel alloc]initWithFrame:AutoRectMake(100, 65, DEFAULT_WIDTH - 100, 30, WTFAutoLayoutTypeText)];
    courseName.text = _course.courseName;
    [self.view addSubview:courseName];
    //
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showInstructorDetailsVc)];
    [viewInstructor addGestureRecognizer:singleTap];
    //老师简介label
    PxhLabel* teacherShort = [[PxhLabel alloc] init];
    teacherShort.text = _course.TeacherdescShort;
    teacherShort.frame = AutoRectMake(10, kVideoViewHeight + kInstructorDetailViewHeight, DEFAULT_WIDTH - 10, 3200, WTFAutoLayoutTypeText);
    [teacherShort sizeToFit];
    teacherShort.font = [UIFont fontWithName:@"Helvetica" size:14];
    [self.view addSubview:teacherShort];
    teacherShort.backgroundColor = [UIColor clearColor];
    _kInstructorDetailViewHeight = kInstructorDetailViewHeight + teacherShort.size.height;
}

- (void)addCourseClassesView
{
    UIView *courseClassesView = [[UIView alloc] initWithFrame:AutoRectMake(0, kVideoViewHeight + _kInstructorDetailViewHeight, DEFAULT_WIDTH, kCourseClassesViewHeight, WTFAutoLayoutTypePic)];
    
    if (!_course.courseClasses.count) return;
    
    UILabel *_lblLessonHours = [[UILabel alloc] initWithFrame:AutoRectMake(20, 0, 100, 20, WTFAutoLayoutTypeBar)];
    _lblLessonHours.font = [UIFont fontWithName:nil size:FONT_SIZE_TEXT_DEFAULT];
    [courseClassesView addSubview:_lblLessonHours];
    _lblLessonHours.text = [NSString stringWithFormat:@"课时 : %@", _course.lessonHours];
    
    _lblClassPrice = [[UILabel alloc] initWithFrame:AutoRectMake(20, 20, 100, 20, WTFAutoLayoutTypeBar)];
    _lblClassPrice.textColor = [UIColor orangeColor];
    _lblClassPrice.font = [UIFont fontWithName:nil size:FONT_SIZE_TEXT_DEFAULT];
    [courseClassesView addSubview:_lblClassPrice];
    if (_course.courseClasses.count)
        _lblClassPrice.text = [NSString stringWithFormat:@"价格 : ￥%@", [_course.courseClasses[_course.selectedClsIndex - 1] classPrice]];
    
//    UIButton *btnPurchase = [[UIButton alloc] initWithFrame:AutoRectMake(120, 0, 100, 24, WTFAutoLayoutTypeBar)];
//    [btnPurchase addTarget:self action:@selector(purchase) forControlEvents:UIControlEventTouchUpInside];
//    [btnPurchase setTitle:@"立即购买" forState:UIControlStateNormal];
//    [btnPurchase setBackgroundImage:[UIImage imageNamed:@"btnBg2"] forState:UIControlStateNormal];
//    [btnPurchase setBackgroundImage:[UIImage imageNamed:@"btnBg2"] forState:UIControlStateSelected];
//    btnPurchase.layer.cornerRadius = 5;
//    [courseClassesView addSubview:btnPurchase];
    
    UILabel *lblClassesPrefix = [[UILabel alloc] initWithFrame:AutoRectMake(20, 40, 50, 20, WTFAutoLayoutTypeBar)];
    lblClassesPrefix.text = @"开课 : ";
    lblClassesPrefix.font = [UIFont fontWithName:nil size:FONT_SIZE_TEXT_DEFAULT];
    [courseClassesView addSubview:lblClassesPrefix];
    
    CGRect rectCls1 = AutoRectMake(70, 40, 40, 20, WTFAutoLayoutTypeBar);
    UILabel *_lblClass1 = [[UILabel alloc] initWithFrame:rectCls1];
    _lblClass1.textAlignment = NSTextAlignmentCenter;
    _lblClass1.font = [UIFont fontWithName:nil size:FONT_SIZE_TEXT_DEFAULT];
    [courseClassesView addSubview:_lblClass1];
    _lblClass1.hidden = YES;
    _btnCls1 = [[UIButton alloc] initWithFrame:rectCls1];
    [_btnCls1 addTarget:self action:@selector(selectCls1) forControlEvents:UIControlEventTouchUpInside];
    _btnCls1.layer.borderColor = [UIColor blackColor].CGColor;
    _btnCls1.layer.borderWidth = 1;
    [courseClassesView addSubview:_btnCls1];
    _btnCls1.hidden = YES;
    if (_course.courseClasses.count > 0)
    {
        _lblClass1.hidden = NO;
        _lblClass1.text = [NSDate monthStringForDateString:[_course.courseClasses[0] classOpenDate]];
        _btnCls1.hidden = NO;
    }
    
    CGRect rectCls2 = AutoRectMake(120, 40, 40, 20, WTFAutoLayoutTypeBar);
    UILabel *_lblClass2 = [[UILabel alloc] initWithFrame:rectCls2];
    _lblClass2.textAlignment = NSTextAlignmentCenter;
    _lblClass2.font = [UIFont fontWithName:nil size:FONT_SIZE_TEXT_DEFAULT];
    [courseClassesView addSubview:_lblClass2];
    _lblClass2.hidden = YES;
    _btnCls2 = [[UIButton alloc] initWithFrame:rectCls2];
    [_btnCls2 addTarget:self action:@selector(selectCls2) forControlEvents:UIControlEventTouchUpInside];
    _btnCls2.layer.borderColor = [UIColor blackColor].CGColor;
    _btnCls2.layer.borderWidth = 1;
    [courseClassesView addSubview:_btnCls2];
    _btnCls2.hidden = YES;
    if (_course.courseClasses.count > 1)
    {
        _lblClass2.hidden = NO;
        _lblClass2.text = [NSDate monthStringForDateString:[_course.courseClasses[1] classOpenDate]];
        _btnCls2.hidden = NO;
    }
    
    
    CGRect rectCls3 = AutoRectMake(170, 40, 40, 20, WTFAutoLayoutTypeBar);
    UILabel *_lblClass3 = [[UILabel alloc] initWithFrame:rectCls3];
    _lblClass3.textAlignment = NSTextAlignmentCenter;
    _lblClass3.font = [UIFont fontWithName:nil size:FONT_SIZE_TEXT_DEFAULT];
    [courseClassesView addSubview:_lblClass3];
    _lblClass3.hidden = YES;
    _btnCls3 = [[UIButton alloc] initWithFrame:rectCls3];
    [_btnCls3 addTarget:self action:@selector(selectCls3) forControlEvents:UIControlEventTouchUpInside];
    _btnCls3.layer.borderColor = [UIColor blackColor].CGColor;
    _btnCls3.layer.borderWidth = 1;
    [courseClassesView addSubview:_btnCls3];
    _btnCls3.hidden = YES;
    if (_course.courseClasses.count > 2)
    {
        _lblClass3.hidden = NO;
        _lblClass3.text = [NSDate monthStringForDateString:[_course.courseClasses[2] classOpenDate]];
        _btnCls3.hidden = NO;
    }

    if (_course.selectedClsIndex <= _course.courseClasses.count) {
        switch (_course.selectedClsIndex) {
            case 1:
                _btnCls1.layer.borderColor = [WTFColors defaultIconColor].CGColor;
                break;
            case 2:
                _btnCls2.layer.borderColor = [WTFColors defaultIconColor].CGColor;
                break;
            case 3:
                _btnCls3.layer.borderColor = [WTFColors defaultIconColor].CGColor;
                break;
            default:
                break;
        }
    }

    [self.view addSubview:courseClassesView];
}

- (void)addNavTable
{
    UITableView *navTable = [[UITableView alloc] initWithFrame:AutoRectMake(0, kVideoViewHeight + _kInstructorDetailViewHeight + kCourseClassesViewHeight, DEFAULT_WIDTH, kNavTableHeight, WTFAutoLayoutTypePic) style:UITableViewStylePlain];
    navTable.tableFooterView = [[UIView alloc] init];
    navTable.dataSource = self;
    navTable.delegate = self;
    [self.view addSubview:navTable];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    if (indexPath.row < 2)
    {
        cell.textLabel.text = @[@"课程介绍", @"课程目录"][indexPath.row];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return AutoFloat(50);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
        {
            NSLog(@"courses details");
            CourseIntroductionVC* courseVC = [[CourseIntroductionVC alloc]init];
            courseVC.course = self.course;
            [self.navigationController pushViewController:courseVC animated:NO];
        }
            break;
            
        case 1:
        {
            NSLog(@"advanced courses arrangement");
            CourseArrangementVc *vc = [[CourseArrangementVc alloc] init];
            vc.course = self.course;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (void)showInstructorDetailsVc
{
    NSLog(@"tapped on instructor view");
    
//    if (_courseDetails)
    if (_course)
    {
        CoursedInstructorVc *vc = [[CoursedInstructorVc alloc] init];
        vc.teacher = [[Teacher alloc] initWithCourse:_course];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - http requests

- (void)loadData
{
    [_imgViewTeacherAvatar sd_setImageWithURL:[NSURL URLWithString:_course.teacherHeadPhotoUrl]];
    _lblTeacherName.text = [NSString stringWithFormat:@"讲师 : %@", _course.teacherName];
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        _courseDetails = [PublicCourseDetails courseDetailsByCourseId:_courseId];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [_imgViewTeacherAvatar sd_setImageWithURL:[NSURL URLWithString:_courseDetails.teacherHeadPhotoUrl]];
//            _lblTeacherName.text = [NSString stringWithFormat:@"讲师 : %@", _courseDetails.teacherName];
//        });
//    });
}

- (void)purchase
{
    NSLog(@"purchase");
    //购买  高级课  的次数
    [MobClick event:@"SeniorPages"];
    if (![User isLoggedIn])
    {
        //
        [SignInViewController popUpOnVc:self];
    }
    else
    {
        // to purchase
        ConfirmBillVc *vc = [[ConfirmBillVc alloc] init];
        vc.course = _course;
        [self.navigationController pushViewController:vc animated:NO];
    }
}

- (void)selectCls1
{
    NSLog(@"cls1");
    [self selectClsAtIndex:1];
}
- (void)selectCls2
{
    NSLog(@"cls2");
    
    [self selectClsAtIndex:2];
}
- (void)selectCls3
{
    NSLog(@"cls3");
    [self selectClsAtIndex:3];
    
}

- (void)selectClsAtIndex:(NSInteger)index
{
    _lblClassPrice.text = [NSString stringWithFormat:@"价格 : ￥%@", [_course.courseClasses[index - 1] classPrice]];
    _btnCls1.layer.borderColor = [UIColor blackColor].CGColor;
    _btnCls2.layer.borderColor = [UIColor blackColor].CGColor;
    _btnCls3.layer.borderColor = [UIColor blackColor].CGColor;
    switch (index) {
        case 1:
            _btnCls1.layer.borderColor = [WTFColors defaultIconColor].CGColor;
            break;
        case 2:
            _btnCls2.layer.borderColor = [WTFColors defaultIconColor].CGColor;
            break;
        case 3:
            _btnCls3.layer.borderColor = [WTFColors defaultIconColor].CGColor;
            break;
        default:
            break;
    }
}

@end
